package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateHomeTab
//@description: 创建HomeTab记录
//@param: tab model.HomeTab
//@return: err error

func CreateHomeTab(tab model.HomeTab) (err error) {
	err = global.GVA_DB.Create(&tab).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteHomeTab
//@description: 删除HomeTab记录
//@param: tab model.HomeTab
//@return: err error

func DeleteHomeTab(tab model.HomeTab) (err error) {
	err = global.GVA_DB.Delete(&tab).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteHomeTabByIds
//@description: 批量删除HomeTab记录
//@param: ids request.IdsReq
//@return: err error

func DeleteHomeTabByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.HomeTab{},"id in ?",ids.Ids).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateHomeTab
//@description: 更新HomeTab记录
//@param: tab *model.HomeTab
//@return: err error

func UpdateHomeTab(tab model.HomeTab) (err error) {
	err = global.GVA_DB.Save(&tab).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetHomeTab
//@description: 根据id获取HomeTab记录
//@param: id uint
//@return: err error, tab model.HomeTab

func GetHomeTab(id uint) (err error, tab model.HomeTab) {
	err = global.GVA_DB.Where("id = ?", id).First(&tab).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetHomeTabInfoList
//@description: 分页获取HomeTab记录
//@param: info request.HomeTabSearch
//@return: err error, list interface{}, total int64

func GetHomeTabInfoList(info request.HomeTabSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&model.HomeTab{})
    var tabs []model.HomeTab
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.Title != "" {
        db = db.Where("`title` LIKE ?","%"+ info.Title+"%")
    }
    if info.MaterialId != "" {
        db = db.Where("`material_id` LIKE ?","%"+ info.MaterialId+"%")
    }
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&tabs).Error
	return err, tabs, total
}